<?php
/**
 * WpMySqlLog.php
 * 
 * This file contains the WpMySqlLog class which provides a class to store log messages in a mysql database.
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base.wplog
 */

/**
 * The interface IWpLog to implement
 */
require_once (dirname(__FILE__).'/IWpLog.php');

/**
 * The abstract class WpLogBase to extend
 */
require_once(dirname(__FILE__).'/WpLogBase.php');

/**
 * Write log entries to a MySql database
 * 
 * How to use:
 * <code>
 * $log = new WpMySqlLog('localhost', 'user', 'password', 'logdb', 'logtable');
 * $log->logError('location', 'anonymous', 'An error', 'Error description', '001', 1);
 * </code>
 * 
 * The targeted table must contain the following columns:
 * type - varchar
 * location - varchar
 * user - varchar
 * datetime - varchar
 * title - varchar
 * body - text
 * number - varchar
 * level - int
 * 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base.wplog
 */
class WpMySqlLog extends WpLogBase implements IWpLog
{
  /**
   * The database host
   */
	private $host;
  
  /**
   * The database user
   */
	private $user;
  
  /**
   * The database password
   */
	private $password;
  
  /**
   * The log database
   */
	private $db;
  
  /**
   * The log table name
   */
	private $table;
  
  /**
   * Determines wether to write in realtime or not
   */
	private $realtime;
  
  /**
   * The log cache
   */
	private $arrSql = array();
	
	/**
	 * The class constructor
	 * @param $host The database host
	 * @param $user The user to connect to database
	 * @param $password The users password
	 * @param $db The database containing the log table
	 * @param $table The name of the log table
	 * @param $realtime True to write entries immediately, false to write to db in destructor
	 */
	public function __construct($host, $user, $password, $db, $table, $realtime=false)
	{
		parent::__construct();
		$this->host = $host;
		$this->user = $user;
		$this->password = $password;
		$this->db = $db;
		$this->table = $table;
		$this->realtime = $realtime;
	}
	
	/**
	 * The class destructor
	 */
	public function __destruct()
	{
		$loc = basename(__FILE__).'/'.__METHOD__;
		if (count($this->arrSql) < 1) {
			return;
		}
		
		//-- Db connection --
		$c = mysql_connect($this->host, $this->user, $this->password);
		if (!$c) {
			$this->fireEvent('error', array($loc, 'Error connecting to mysql server: '.$this->host));
			return false;
		}
		if (!mysql_select_db($this->db, $c)) {
			$this->fireEvent('error', array($loc, 'Error selecting database: '.$this->db));
			return false;
		}
		
		//-- Transaction --
		mysql_query('begin', $c);
		foreach ($this->arrSql as $strSql) {
		
			if (!mysql_query($strSql, $c)) {
				$this->fireEvent('error', array($loc, 'Error inserting the log message: '.mysql_error()));
				mysql_query('rollback');
				return false;
			}
		}
		mysql_query('commit', $c);
	}
	
	/**
   * Logs an entry
   * @param object $logObject The log entry to save
   */
	protected function _log2x($logObject)
	{
		$loc = basename(__FILE__).'/'.__METHOD__;
		
		if (!array_key_exists('level', $logObject)) {
			$level = 1;
		} else {
			$level = $logObject['level'];
		}
		
		$strSql = 'INSERT INTO '.mysql_escape_string($this->table).'
			(type, location, user, datetime, title, body, number, level) VALUES ('.
			'\''.mysql_escape_string($logObject['type']).'\','.
			'\''.mysql_escape_string($logObject['location']).'\','.
			'\''.mysql_escape_string($logObject['user']).'\','.
			'\''.mysql_escape_string($logObject['date']).'\','.
			'\''.mysql_escape_string($logObject['title']).'\','.
			'\''.mysql_escape_string($logObject['body']).'\','.
			'\''.mysql_escape_string($logObject['number']).'\','.
			''.$level.')';
		
		if ($this->realtime !== true) {
			$this->arrSql[] = $strSql;
			return true;
		}
		
		//-- Realtime logging --
		$c = mysql_connect($this->host, $this->user, $this->password);
		if (!$c) {
			$this->fireEvent('error', array($loc, 'Error connecting to mysql server: '.$this->host));
			return false;
		}
		
		if (!mysql_select_db($this->db, $c)) {
			$this->fireEvent('error', array($loc, 'Error selecting database: '.$this->db));
			return false;
		}
		
		if (!mysql_query($strSql, $c)) {
			$this->fireEvent('error', array($loc, 'Error inserting the log message: '.mysql_error()));
			return false;
		}
		return true;
	}
}

/* End of file WpMySqlLog.php */
/* Location: ./wildpack/base/wplog/WpMySqlLog.php */ 
